//
//  JCRepayProgressController.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/3/30.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCRepayProgressController.h"
#import "JCProgressListCell.h"
#import "JCProgressDetailsController.h"
#import "ProgressModel.h"



@interface JCRepayProgressController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * progressTableView;
@property (nonatomic, strong) NSMutableArray * arrCard;

@end

@implementation JCRepayProgressController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"还款进度查询";
    self.arrCard = [NSMutableArray array];
//    [self refresh];
    [self.view addSubview:self.progressTableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self DoRequsetRepay_plans];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrCard.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JCProgressListCell * cell = [JCProgressListCell tableViewCellWithTableView:tableView];
    cell.progressModel = self.arrCard[indexPath.row];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return __I6Height(140) + __I6Height(15);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JCProgressDetailsController * detailsVC = [[JCProgressDetailsController alloc] init];
    detailsVC.model = self.arrCard[indexPath.row];
    [self.navigationController pushViewController:detailsVC animated:YES];
}


// 侧滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    ProgressModel * model = [[ProgressModel alloc] init];
    model = self.arrCard[indexPath.row];
    if ([model.repay_status isEqualToString:@"Repaying"]) {
        return YES;
    }else{
        return NO;
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
/**
 *  修改Delete按钮文字为“删除”
 */
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"取消代还计划";
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   
    ProgressModel * model = [[ProgressModel alloc] init];
    model = self.arrCard[indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        [self DoRequsetH_stop_repay_plan:model.repayPlanId];

    }
}

#pragma mark - 懒加载
- (UITableView *)progressTableView{
    if (!_progressTableView) {
        
        _progressTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _progressTableView.delegate = self;
        _progressTableView.dataSource = self;
        _progressTableView.backgroundColor = [UIColor whiteColor];
        _progressTableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    }
    
    return _progressTableView;
}

// 网络请求
- (void)DoRequsetRepay_plans{
    
    [PPNetworkHelper GET:H_repay_plans parameters:nil cache:NO encrypt:NO decode:NO responseCache:^(id responseCache) {
        
    } success:^(id result) {
        if ([result[@"success"] boolValue] == YES) {
            [self.arrCard removeAllObjects];
            for (NSDictionary * dic in result[@"data"]) {
                [self.arrCard addObject:[ProgressModel mj_objectWithKeyValues:dic]];
            }
        }else{
            [self.view  makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        [self.progressTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

//  取消还贷计划
- (void)DoRequsetH_stop_repay_plan:(NSString *)planId{
    [PPNetworkHelper GET:H_stop_repay_plan parameters:@{@"repayPlanId":planId} cache:NO encrypt:NO decode:NO responseCache:^(id responseCache) {
        
    } success:^(id result) {
        NSLog(@"result :%@",result);
        if ([result[@"success"] boolValue] == YES) {
            [self.view  makeToast:@"取消成功" duration:2.f position:@"center"];

            [self DoRequsetRepay_plans];
            
        }else{
            [self.view  makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)back:(UIBarButtonItem *)item{
    if (self.type == 1) {
        UIViewController *controller = self.navigationController.viewControllers[1];
        
        [self.navigationController popToViewController:controller animated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)refresh{
    
    @weakify(self)
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.progressTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self DoRequsetRepay_plans];
        [self.progressTableView.mj_header endRefreshing];
        // 马上进入刷新状态
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
